package com.lab.webSocket.controller;

import com.lab.webSocket.dto.JsonMsgDto;
import com.lab.webSocket.dto.UserDto;
import com.lab.webSocket.service.AddUserService;
import com.lab.webSocket.service.ChatService;
import com.lab.webSocket.webSocket.WebSocketEndPoint;
import com.lab.webSocket.webSocket.dto.TextChatResultDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.socket.TextMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Created by xu_jiaHui on 2017/8/27.
 */
@Controller
@RequestMapping("/ws_socket")
public class SocketController {
    @Resource
    private ChatService chatService;
    @Resource
    private AddUserService addUserService;


    @RequestMapping("/sendMessage")
    @ResponseBody
    public JsonMsgDto sendMessage(TextChatResultDto chatResultDto) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setSuccess(true);
            jsonMsgDto.setJsonData(chatService.sendMessage(chatResultDto));
        } catch (Exception e) {
            jsonMsgDto.setSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }


    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public JsonMsgDto updateUserInfo(UserDto userDto) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setSuccess(true);
            jsonMsgDto.setJsonData(addUserService.updateUserInfo(userDto));
        } catch (Exception e) {
            jsonMsgDto.setSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }


    @RequestMapping("/getUserInfo")
    @ResponseBody
    public JsonMsgDto getUserInfo(String userAccount) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setSuccess(true);
            jsonMsgDto.setJsonData(addUserService.getUserInfo(userAccount));
        } catch (Exception e) {
            jsonMsgDto.setSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

}
